﻿using Cinchoo.Core;
using Cinchoo.Core.Configuration;
using Cinchoo.Core.IO;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ChoBulkFileRenamer
{
    internal class ChoPlugInsDefFileManager
    {
        public string PlugInsDefFilePath;
        private string _findPlugInDefText;
        private string _replacePlugInDefText;

        public string FindPlugInDefText
        {
            get { return _findPlugInDefText; }
            set
            {
                _findPlugInDefText = value;
                SavePlugInsDef();
            }
        }

        public string ReplacePlugInDefText
        {
            get { return _replacePlugInDefText; }
            set
            {
                _replacePlugInDefText = value;
                SavePlugInsDef();
            }
        }

        public ChoPlugInsDefFileManager(string plugInsDefFilePath = null)
        {
            PlugInsDefFilePath = plugInsDefFilePath;
            LoadPlugInsDef();
        }

        private void LoadPlugInsDef()
        {
            FindPlugInDefText = null;
            ReplacePlugInDefText = null;

            if (!PlugInsDefFilePath.IsNullOrWhiteSpace() && File.Exists(PlugInsDefFilePath))
            {
                XDocument doc = XDocument.Load(PlugInsDefFilePath);
                if (doc != null || doc.Root != null)
                {
                    foreach (XElement element in doc.Root.Elements())
                    {
                        if (element.Name.LocalName == "findPlugIns")
                        {
                            foreach (XElement childElement in element.Elements())
                            {
                                FindPlugInDefText = childElement.ToString();
                                break;
                            }
                        }
                        else if (element.Name.LocalName == "replacePlugIns")
                        {
                            foreach (XElement childElement in element.Elements())
                            {
                                ReplacePlugInDefText = childElement.ToString();
                                break;
                            }
                        }
                    }
                }
            }
        }

        private void SavePlugInsDef()
        {
            if (PlugInsDefFilePath.IsNullOrWhiteSpace())
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "PlugIn files|*{0}".FormatString(App.PlugInFileExt);

                var result = dlg.ShowDialog();

                if (result == true)
                    PlugInsDefFilePath = dlg.FileName;
                else
                {
                    PlugInsDefFilePath = null;
                    return;
                }
            }
            File.WriteAllText(PlugInsDefFilePath,
                @"<bulkFileRenamePlugIns><findPlugIns>{0}</findPlugIns><replacePlugIns>{1}</replacePlugIns></bulkFileRenamePlugIns>".FormatString(_findPlugInDefText, _replacePlugInDefText).IndentXml());
        }

        public string GetFindPlugInFilePath()
        {
            if (_findPlugInDefText.IsNullOrWhiteSpace()) return null;

            string tmpFilePath = ChoPath.GetTempFileName();
            File.WriteAllText(tmpFilePath, _findPlugInDefText);
            return tmpFilePath;
        }

        public string GetReplacePlugInFilePath()
        {
            if (_replacePlugInDefText.IsNullOrWhiteSpace()) return null;

            string tmpFilePath = ChoPath.GetTempFileName();
            File.WriteAllText(tmpFilePath, _replacePlugInDefText);
            return tmpFilePath;
        }
    }
}
